
package logic.forms;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import logic.beans.Behavior;
import logic.beans.Disease;
import logic.beans.Drug;
import logic.beans.Food;
import logic.beans.Treatment;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import controller.SEISaudeDefaultFacade;

public class TreatmentForm extends ElementForm<Treatment> {
	private static final long serialVersionUID = 1L;
	
	@Override
	public ActionErrors validate(ActionMapping mapping,HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		
		if (isFieldEmpty(getElement().getDescription())) {
			errors.add("description",new ActionMessage("treatment.register.error.description.empty"));
		}
		
//		if (isFieldEmpty(getElement().getDisease().getName())) {
//			errors.add("disease",new ActionMessage("treatment.register.error.disease.empty"));
//		}
		
		return errors;
	}
	
	@Override
	public List<String> getEmptyPropertyNames() {
		List<String> result = new ArrayList<String>();
		
		if (isFieldEmpty(getElement().getDescription()))
			result.add("description");
		if (getElement().getDisease() == null)
			result.add("disease");
		if (getElement().getBehaviors().isEmpty())
			result.add("behaviors");
		if (getElement().getDrugs().isEmpty())
			result.add("drugs");
		if (getElement().getFoods().isEmpty())
			result.add("foods");
		
		return result;
	}
	
	@Override
	public void setElementFromRequest(HttpServletRequest request) {
		SEISaudeDefaultFacade<Disease,Long> diseaseFacade = SEISaudeDefaultFacade.getInstance(Disease.class);
		String diseasesComboValue = request.getParameter("diseasesCombo");
		if (diseasesComboValue != null && !diseasesComboValue.equals("null")) {
			getElement().setDisease(diseaseFacade.getElementById(Long.parseLong(diseasesComboValue)));
		}
		
		SEISaudeDefaultFacade<Behavior,Long> behaviorFacade = SEISaudeDefaultFacade.getInstance(Behavior.class);
		Set<Behavior> behaviorList = new HashSet<Behavior>();
		String sizeParam = request.getParameter("behaviorsComboListSize");
		for (int i = 0; i < Integer.parseInt(sizeParam == null ? "0" : sizeParam); i++) {
			String eachComboValue = request.getParameter("behaviorsCombo" + String.valueOf(i));
			if (!eachComboValue.equals("null")) {
				behaviorList.add(behaviorFacade.getElementById(Long.parseLong(eachComboValue)));
			}
		}
		getElement().setBehaviors(behaviorList);
		
		SEISaudeDefaultFacade<Drug,Long> drugFacade = SEISaudeDefaultFacade.getInstance(Drug.class);
		Set<Drug> drugList = new HashSet<Drug>();
		sizeParam = request.getParameter("drugsComboListSize");
		for (int i = 0; i < Integer.parseInt(sizeParam == null ? "0" : sizeParam); i++) {
			String eachComboValue = request.getParameter("drugsCombo" + String.valueOf(i));
			if (!eachComboValue.equals("null")) {
				drugList.add(drugFacade.getElementById(Long.parseLong(eachComboValue)));
			}
		}
		getElement().setDrugs(drugList);
		
		SEISaudeDefaultFacade<Food,Long> foodFacade = SEISaudeDefaultFacade.getInstance(Food.class);
		Set<Food> foodList = new HashSet<Food>();
		sizeParam = request.getParameter("foodsComboListSize");
		for (int i = 0; i < Integer.parseInt(sizeParam == null ? "0" : sizeParam); i++) {
			String eachComboValue = request.getParameter("foodsCombo" + String.valueOf(i));
			if (!eachComboValue.equals("null")) {
				foodList.add(foodFacade.getElementById(Long.parseLong(eachComboValue)));
			}
		}
		getElement().setFoods(foodList);
	}
	
	@Override
	public void setRequestFromElement(HttpServletRequest request) {
		request.setAttribute("diseaseList",SEISaudeDefaultFacade.getInstance(Disease.class).getAllElements());
		request.setAttribute("behaviorList",SEISaudeDefaultFacade.getInstance(Behavior.class).getAllElements());
		request.setAttribute("drugList",SEISaudeDefaultFacade.getInstance(Drug.class).getAllElements());
		request.setAttribute("foodList",SEISaudeDefaultFacade.getInstance(Food.class).getAllElements());
	}
}
